package base.email;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.json.JSONObject;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.web.context.WebApplicationContext;

/**
 * spring的邮件发送例子
 * 
 * @author Amigo Xie(xiexingxing1121@126.com)
 * @since 2007/04/28 11:30
 */

public class SpringMail {

  public JavaMailSender sender;

  public SpringMail(WebApplicationContext wac) {
    sender = (JavaMailSender) wac.getBean("mailSender");
  }

  /**
   * 测试发送只有文本信息的简单测试
   * 
   * @param sender
   *          邮件发送器
   * @throws Exception
   */
  public void sendTextMail(JavaMailSender sender, Map<String, String> map)
      throws Exception {
    SimpleMailMessage mail = new SimpleMailMessage();
    mail.setTo(map.get("emailTo"));
    mail.setFrom(map.get("emailFrom"));
    mail.setSubject(map.get("title"));
    mail.setText(map.get("content"));
    sender.send(mail);
  }

  /**
   * 发送带附件的邮件
   * 
   * @param sender
   *          邮件发送器
   * @throws Exception
   */
  private void sendMimeMessage(final JavaMailSender sender) throws Exception {
    // 附件文件集合
    final List files = new ArrayList();
    MimeMessagePreparator mimeMail = new MimeMessagePreparator() {
      public void prepare(MimeMessage mimeMessage) throws MessagingException {
        mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(
            "xiexingxing1121@126.com"));
        mimeMessage.setFrom(new InternetAddress("xiexingxing1121@126.com"));
        mimeMessage.setSubject("Spring发送带附件的邮件", "gb2312");

        Multipart mp = new MimeMultipart();

        // 向Multipart添加正文
        MimeBodyPart content = new MimeBodyPart();
        content.setText("内含spring邮件发送的例子，请查收!");

        // 向MimeMessage添加（Multipart代表正文）
        mp.addBodyPart(content);
        files.add("src/SpringMail.java");
        files.add("src/applicationContext.xml");

        // 向Multipart添加附件
        Iterator it = files.iterator();
        while (it.hasNext()) {
          MimeBodyPart attachFile = new MimeBodyPart();
          String filename = it.next().toString();
          FileDataSource fds = new FileDataSource(filename);
          attachFile.setDataHandler(new DataHandler(fds));
          attachFile.setFileName(fds.getName());
          mp.addBodyPart(attachFile);
        }

        files.clear();

        // 向Multipart添加MimeMessage
        mimeMessage.setContent(mp);
        mimeMessage.setSentDate(new Date());
      }
    };

    // 发送带附件的邮件
    sender.send(mimeMail);

    System.out.println("成功发送带附件邮件!");
  }
}